from sys import stdin
def adding_one(n):
	return n+1

class Board:
	def __init__(self):
		# init board to an empty 3x3 grid
		self._board = [[0 for i in range(3)] for j in range(3)]
		
	def is_cell_empty(self, x, y):
		return self._board[x][y] == 0
		
	def is_valid_cell(self, x, y):
		return not (x < 0 or x >= 3 or y < 0 or y >= 3)
		
	def choose_square(self, player, x, y):
		self._board[x][y] = player
	
	def print_board(self):
		# print the board
		for row in range(3):
			for col in range(3):
				if self._board[row][col] == 1:
					print 1,
				elif self._board[row][col] == 2:
					print 2,
				else:
					print 0,
			print
	
	def find_winner(self):
		"""Returns 0 if nobody is winning, 1 if player1 wins, and 2 if player2 wins"""
		# TODO: Katlynn! Please implement me!
		p1_owns_top_row = self._board[0][0] == 1 and self._board[0][1] == 1 and self._board[0][2] == 1
		p1_owns_mid_row = self._board[1][0] == 1 and self._board[1][1] == 1 and self._board[1][2] == 1
		p1_owns_bot_row = self._board[2][0] == 1 and self._board[2][1] == 1 and self._board[2][2] == 1
		p1_owns_rd_diag = self._board[0][0] == 1 and self._board[1][1] == 1 and self._board[2][2] == 1
		p1_owns_ld_diag = self._board[0][2] == 1 and self._board[1][1] == 1 and self._board[2][0] == 1
		p1_owns_r_col = self._board[0][0] == 1 and self._board[1][0] == 1 and self._board[2][0] == 1
		p1_owns_mid_col = self._board[0][1] == 1 and self._board[1][1] == 1 and self._board[1][2] == 1
		p1_owns_l_col = self._board[0][2] == 1 and self._board[1][2] == 1 and self._board[2][2] == 1
		if p1_owns_top_row or p1_owns_mid_row or p1_owns_bot_row or p1_owns_rd_diag or p1_owns_ld_diag or p1_owns_r_col or p1_owns_mid_col or p1_owns_l_col:
			return 1 # nobody ever wins, i don't give a damn FIX THIS
		
		p2_owns_top_row = self._board[0][0] == 2 and self._board[0][1] == 2 and self._board[0][2] == 2
		p2_owns_mid_row = self._board[1][0] == 2 and self._board[1][1] == 2 and self._board[1][2] == 2
		p2_owns_bot_row = self._board[2][0] == 2 and self._board[2][1] == 2 and self._board[2][2] == 2
		p2_owns_rd_diag = self._board[0][0] == 2 and self._board[1][1] == 2 and self._board[2][2] == 2
		p2_owns_ld_diag = self._board[0][2] == 2 and self._board[1][1] == 2 and self._board[2][0] == 2
		p2_owns_r_col = self._board[0][0] == 2 and self._board[1][0] == 2 and self._board[2][0] == 2
		p2_owns_mid_col = self._board[0][1] == 2 and self._board[1][1] == 2 and self._board[1][2] == 2
		p2_owns_l_col = self._board[0][2] == 2 and self._board[1][2] == 2 and self._board[2][2] == 2
		if p2_owns_top_row or p2_owns_mid_row or p2_owns_bot_row or p2_owns_rd_diag or p2_owns_ld_diag or p2_owns_r_col or p2_owns_mid_col or p2_owns_l_col:
			return 2 # nobody ever wins, i don't give a damn FIX THIS
		else:
			return 0	
	
if __name__ == '__main__':
	print 'Creating fresh board'
	board = Board()
	board.print_board()
	
	current_player = 1
	while board.find_winner() == 0:
		print 'Where would you like to go, Player', current_player, '\b?'
		line = raw_input()
		coords = line.split()
		if len(coords) != 2:
			print 'please enter two coordinate values between 1 and 3'
			continue
			
		# convert to 0-indexed coords needed by board class
		x = int(coords[0]) - 1
		y = int(coords[1]) - 1
		if not board.is_valid_cell(x, y):
			print 'You entered invalid coordinates', x + 1, y + 1
			continue
		if not board.is_cell_empty(x, y):
			print 'please choose an unoccupied square, player', current_player
			continue
			
		board.choose_square(current_player, x, y)
		current_player = (current_player % 2) + 1
		board.print_board()
		
	print 'player', (current_player % 2) + 1, 'is the winner!'

# for katlynn: what does current_player = (current_player % 2) + 1 do?
# how do you test if a number is even
# what could break here?